<template>
    <el-card>
         <!-- 路径导航 -->
        <div slot="header">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item :to="{ path: '/home' }"> <i class="el-icon-house"></i> 首页</el-breadcrumb-item>
                <el-breadcrumb-item>订单管理</el-breadcrumb-item>
                <el-breadcrumb-item>订单列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
         <!-- 主体内容 -->
         <br>
        <!-- 筛选区域 -->
        <div class="filter-bar">
            <!-- 筛选表单 -->
            <div class="l-form">
               <el-form :model="searchForm" label-width="80px" :inline="true" size="small">
                   <el-form-item label="用户ID">
                       <el-input v-model="searchForm.user_id" placeholder="请输入用户ID"></el-input>
                   </el-form-item>
                    <el-form-item label="支付状态">
                       <el-select v-model="searchForm.pay_status"   >
                           <el-option  value="" label="全部状态"> </el-option>
                           <el-option  value="1" label="已付款"> </el-option>
                           <el-option  value="0" label="未付款"> </el-option>
                       </el-select>
                       
                   </el-form-item>
                    <el-form-item label="发货状态">
                        <el-select v-model="searchForm.is_send"   >
                           <el-option  value="" label="全部状态"> </el-option>
                           <el-option  value="是" label="是"> </el-option>
                           <el-option  value="否" label="否"> </el-option>
                       </el-select>
                   </el-form-item>
                   <el-form-item>
                       <el-button type="primary" @click="getOrderList">查询</el-button>
                       <el-button @click="resetSearch">重置</el-button>
                   </el-form-item>
               </el-form>
            </div>
        </div>
         <!-- 表格区域 -->
         <div class="table-box">
             <!-- 表格 -->
             <el-table :data="orderList" border >
                 <el-table-column label="序号" type="index" width="80"></el-table-column>
                 <el-table-column label="订单编号" prop="order_number" width="300"></el-table-column>
                 <el-table-column label="订单价格" prop="order_price"></el-table-column>
                 <el-table-column label="用户ID" prop="user_id"></el-table-column>
                 <el-table-column label="支付状态" >
                     <template slot-scope="scope">
                         <el-tag type="warning" v-if="scope.row.pay_status === '0'" size="normal">未付款</el-tag>
                         <el-tag type="success" v-if="scope.row.pay_status === '1'" size="normal">已付款</el-tag>
                     </template>
                 </el-table-column>
                 <el-table-column label="是否发货" >
                     <template slot-scope="scope">
                         <el-button type="primary" size="small" circle  plain v-if=" scope.row.is_send==='是'" icon='el-icon-check'></el-button>
                         <el-button type="warning" size="small" circle  plain v-if=" scope.row.is_send==='否'" icon='el-icon-close'></el-button>
                     </template>
                 </el-table-column>
                 <el-table-column label="下单时间"  width="250">
                     <template slot-scope="scope">
                         {{ scope.row.create_time | formateTime }}
                     </template>
                 </el-table-column>
                 <el-table-column label="操作" >
                     <template slot-scope="scope">
                         <el-tooltip  effect="dark" content="修改" placement="top">
                            <el-button 
                                type="success" 
                                size="small" 
                                @click="editOrder(scope.row)" 
                                circle 
                                icon="el-icon-edit" 
                            ></el-button>
                        </el-tooltip>
                        <el-tooltip  effect="dark" content="查看物流信息" placement="top">
                            <el-button 
                                type="warning" 
                                size="small"  
                                @click="seeExpress(scope.row)"  
                                circle 
                                icon="el-icon-truck"
                            ></el-button>
                        </el-tooltip>
                     </template>
                 </el-table-column>
             </el-table>
             <!-- 分页器 -->
             <el-pagination
                 @current-change="currentChange"
                 :current-page.sync="searchForm.pagenum"
                 :page-size="searchForm.pagesize"
                 layout="total, prev, pager, next, jumper"
                 :total="total" background>
              >
             </el-pagination>

             <!-- 查看物流信息的弹框 -->
             <el-dialog
                 title="物流信息"
                 :visible.sync="addressDialogShow"
                 width="60%"
                >
                <el-row>
                    <el-col :span="20" :offset="3">
                        <el-timeline :reverse="false">
                            <el-timeline-item
                            v-for="(item, index) in expressInfo"
                            :key="index"
                            :color="item.color"
                            :size="item.size"
                            :icon="item.icon"
                            :timestamp="item.time">
                            {{item.context}}
                            </el-timeline-item>
                        </el-timeline>
                    </el-col>
                </el-row>
                 <span slot="footer">
                     <el-button type="primary" @click="addressDialogShow = false">关闭</el-button>
                 </span>
             </el-dialog>

             <!-- 修改地址弹框 -->
             <el-dialog
                 title="修改收货地址"
                 :visible.sync="editAddressDialogShow"
                 width="40%"
                 @close="$refs.addressForm.resetFields()"
                >
                 <el-form :model="addressForm" ref="addressForm" :rules="addressFormRules" label-width="80px" >
                     <el-form-item label="省市县" prop="address_list">
                         <el-cascader :options="cityData" v-model="addressForm.address_list">
                         </el-cascader>
                     </el-form-item>
                     <el-form-item label="详细地址" prop="detail_info">
                         <el-input v-model="addressForm.detail_info" type="textarea"></el-input>
                     </el-form-item>
                 </el-form>
                 <span slot="footer">
                     <el-button @click="editAddressDialogShow = false">取消</el-button>
                     <el-button type="primary" @click="editAddressSubmit">确定</el-button>
                 </span>
             </el-dialog>
             

         </div>
    </el-card>
</template>
<script>
import cityData from "@/utils/citydata"
    let defaultItem = {
        user_id:'',
        pagesize:10,
        pagenum:1,
        pay_status:'',
        is_send:''
    }
    export default {
        data() {
            return {
                // 搜索表单的变量
                searchForm:{...defaultItem},
                total:0, // 总数
                orderList:[], // 订单列表
                addressDialogShow:false,  // 物流信息弹框控制变量
                editAddressDialogShow:false,  // 物流信息弹框控制变量
                expressInfo:[],  // 物流信息
                addressForm:{
                    address_list:[],  // 省市县
                    detail_info:''  // 详细地址
                },
                addressFormRules:{
                    address_list:[{required:true,message:'请选择省市县',trigger:'blur'}],
                    detail_info:[{required:true,message:'请输入详细地址',trigger:'blur'}]
                },
                cityData  // 省市县数据
            }
        },
        created () {
            this.getOrderList()
        },
        methods: {
            async getOrderList() {
                // 发送请求
                let { data:res } = await this.$axios.get('orders',{
                    params:this.searchForm
                })
                // 错误处理
                if(res.meta.status !== 200) return this.$message.error(res.meta.msg)
                // 成功赋值
                this.orderList = res.data.goods
                this.total = res.data.total 
            },
            // 重置搜索框事件
            resetSearch(){
                this.searchForm = {...defaultItem}
                this.getOrderList()
            },
            // 页码更换事件
            currentChange(val){
                this.searchForm.pagenum = val
                this.getOrderList()
            },
            // 查看物流信息
            async seeExpress(row){
                if(row.is_send === '是'){
                    // 发货了
                    // console.log(row);
                    // let { data:res } = await this.$axios.get('/kuaidi/'+row.order_number)
                    // let { data:res } = await this.$axios.get('kuaidi/1106975712662')
                    // if(res.meta.status !== 200) return this.$message.error(res.meta.msg)
                    // this.expressInfo = res.data
                    this.expressInfo = [{
                            "time": "2018-05-10 09:39:00",
                            "ftime": "2018-05-10 09:39:00",
                            "context": "已签收,感谢使用顺丰,期待再次为您服务",
                            "location": "",
                            "icon": 'el-icon-location-information',
                            "color": '#0bbd87',
                            "size": 'large',
                            },
                            {
                            "time": "2018-05-10 08:23:00",
                            "ftime": "2018-05-10 08:23:00",
                            "context": "[北京市]北京海淀育新小区营业点派件员 顺丰速运 95338正在为您派件",
                            "location": ""
                            },
                            {
                            "time": "2018-05-10 07:32:00",
                            "ftime": "2018-05-10 07:32:00",
                            "context": "快件到达 [北京海淀育新小区营业点]",
                            "location": ""
                            },
                            {
                            "time": "2018-05-10 02:03:00",
                            "ftime": "2018-05-10 02:03:00",
                            "context": "快件在[北京顺义集散中心]已装车,准备发往 [北京海淀育新小区营业点]",
                            "location": ""
                            },
                            {
                            "time": "2018-05-09 23:05:00",
                            "ftime": "2018-05-09 23:05:00",
                            "context": "快件到达 [北京顺义集散中心]",
                            "location": ""
                            },
                            {
                            "time": "2018-05-09 21:21:00",
                            "ftime": "2018-05-09 21:21:00",
                            "context": "快件在[北京宝胜营业点]已装车,准备发往 [北京顺义集散中心]",
                            "location": ""
                            },
                            {
                            "time": "2018-05-09 13:07:00",
                            "ftime": "2018-05-09 13:07:00",
                            "context": "顺丰速运 已收取快件",
                            "location": ""
                            },
                            {
                            "time": "2018-05-09 12:25:03",
                            "ftime": "2018-05-09 12:25:03",
                            "context": "卖家发货",
                            "location": ""
                            },
                            {
                            "time": "2018-05-09 12:22:24",
                            "ftime": "2018-05-09 12:22:24",
                            "context": "您的订单将由HLA（北京海淀区清河中街店）门店安排发货。",
                            "location": ""
                            },
                            {
                            "time": "2018-05-08 21:36:04",
                            "ftime": "2018-05-08 21:36:04",
                            "context": "商品已经下单",
                            "location": ""
                            }]
                    this.addressDialogShow = true
                }else{
                    // 没有发货
                    this.$notify.warning({
                        title:"警告！",
                        message:'商品未发货，没有物流信息'
                    })
                }
            },
            // 修改订单地址
            editOrder(row){
                console.log(row);
                this.editAddressDialogShow = true
            },
            // 修改收货地址提交事件
            editAddressSubmit(){
                this.$refs.addressForm.validate(valid=>{
                    if(!valid) return
                    // 发送请求！


                    // 关闭弹框
                     this.editAddressDialogShow = false
                })
            }
        },
    }
</script>

<style lang="less" scoped>

</style>